// 无重复字符的最长子串

let lengthOfLongestSubstring = function (str) {
  let ary = str.split("");
  let maxLen = 0;
  let set = new Set();
  let lenAry = [];
  ary.forEach((item, index) => {
    if (!set.has(item)) {
      set.add(item);
    } else {
      if (item === ary[index - 1]) {
        set.clear();
        set.add(item);
      } else {
        let preIdx = str.slice(0, index).lastIndexOf(item);
        set.clear();
        str.slice(preIdx + 1, index + 1).split("").forEach(ls => {
          set.add(ls);
        })
      }
    }
    lenAry.push(set.size);
  })
  maxLen = Math.max(...lenAry, 0);
  return maxLen;
};

lengthOfLongestSubstring("gaaqfeqlqky");